package com.lyy.mapper;

import com.lyy.entity.Order;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Liangyaoyun
 */@Repository
public interface OrderMapper {

    String list = "select o.order_id, o.order_number, o.total_fee, o.order_info, " +
            "o.create_time, o.modify_time from lesson.order o ";

    /**
     * 添加一条订单记录
     *
     * @param order 添加字段
     * @return 影响条目数
     */
    @Insert("insert into lesson.order ( " +
            "order_number, total_fee, order_info, create_time, modify_time) " +
            "values (" +
            "#{orderNumber}, #{totalFee}, #{orderInfo}, #{createTime}, #{modifyTime})")
    @Options(useGeneratedKeys = true, keyProperty = "orderId")
    int insert(Order order);

    /**
     * 按条件查询订单记录
     *
     * @param condition 查询条件
     * @return 查询失败时返回空List对象
     */
    @Select("<script>" + list +
            "<where> " +
            "<if test='orderId != null'>and o.order_id = #{orderId}</if> " +
            "<if test='orderNumber != null'>and o.order_number = #{orderNumber}</if> " +
            "<if test='totalFee != null'>and o.total_fee = #{totalFee}</if> " +
            "<if test='orderInfo != null'>and o.order_info = #{orderInfo}</if> " +
            "<if test='createTime != null'>and o.create_time = #{createTime}</if> " +
            "<if test='modifyTime != null'>and o.modify_time = #{modifyTime}</if> " +
            "</where>" +
            "</script> ")
    List<Order> select(Order condition);

    /**
     * 按条件修改订单记录
     *
     * @param order     修改字段
     * @param condition 修改条件
     * @return 影响条目数
     */
    @Update("<script>" +
            "update lesson.order " +
            "<set> " +
            "<if test='param1.orderId != null'> order_id = #{param1.orderId},</if> " +
            "<if test='param1.orderNumber!=null'> order_number = #{param1.orderNumber},</if> " +
            "<if test='param1.totalFee != null'> total_fee = #{param1.totalFee},</if> " +
            "<if test='param1.orderInfo != null'> order_info = #{param1.orderInfo},</if> " +
            "<if test='param1.createTime != null'> create_time = #{param1.createTime},</if> " +
            "<if test='param1.modifyTime != null'> modify_time = #{param1.modifyTime},</if> " +
            "</set> " +
            "<where> " +
            "<if test='param2.orderId != null'>and order_id = #{param2.orderId}</if> " +
            "<if test='param2.orderNumber!=null'>and order_number=#{param2.orderNumber}</if> " +
            "<if test='param2.totalFee != null'>and total_fee = #{param2.totalFee}</if> " +
            "<if test='param2.orderInfo != null'>and order_info = #{param2.orderInfo}</if> " +
            "<if test='param2.createTime != null'>and create_time = #{param2.createTime}</if> " +
            "<if test='param2.modifyTime != null'>and modify_time = #{param2.modifyTime}</if> " +
            "</where> " +
            "</script>")
    int update(Order order, Order condition);

    /**
     * 按条件删除订单记录
     *
     * @param condition 删除条件
     * @return 影响条目数
     */
    @Delete("<script>" +
            "delete from lesson.order " +
            "<where> " +
            "<if test='orderId != null'>and order_id = #{orderId}</if> " +
            "<if test='orderNumber != null'>and order_number = #{orderNumber}</if> " +
            "<if test='totalFee != null'>and total_fee = #{totalFee}</if> " +
            "<if test='orderInfo != null'>and order_info = #{orderInfo}</if> " +
            "<if test='createTime != null'>and create_time = #{createTime}</if> " +
            "<if test='modifyTime != null'>and modify_time = #{modifyTime}</if> " +
            "</where> " +
            "</script>")
    int delete(Order condition);

    /**
     * 按订单表主键查询一条订单记录（分步调用专用SQL块）
     *
     * @param orderId 订单表主键
     * @return 查询失败时返回null
     */
    @Select(list + " where o.order_id = #{param1} ")
    Order selectByOrderId(Integer orderId);




























/*     *//*添加*//*
    @Insert("insert into lesson.order(order_number,total_fee,order_info,order_deleted,create_time,modify_time)" +
            "value(#{orderNumber},#{totalFee},#{orderInfo},#{orderDeleted},#{createTime},#{modifyTime})")
    int insertOrder(Order order);

    *//*删除*//*
    @Delete("delete from lesson.order where order_id=#{param1} and order_deleted=0")
    int deleteByOrderId(Integer orderId);

    *//*修改*//*
    String updateOrder="<script> update lesson.order <set>"
            +"<if test='orderNumber!=null'>order_number=#{orderNumber},</if>"
            +"<if test='totalFee!=null'>total_fee=#{totalFee},</if>"
            +"<if test='orderInfo!=null'>order_info=#{orderInfo},</if>"
            +"<if test='orderDeleted!=null'>order_deleted=#{orderDeleted},</if>"
            +"<if test='modifyTime!=null'>modify_time=#{modifyTime},</if>"
            +"<if test='createTime!=null'>create_time=#{createTime},</if>"
            +"</set>" +
            "where order_id=#{orderId} and order_deleted=0" +
            "</script>";
    @Update(updateOrder)
    int  updateByOrderId(Order order);

    *//*根据主键查询*//*
    @Select("select * from lesson.order where order_id=#{param1}")
    Order selectByOrderId(Integer orderId);*/


}
